<template>
  <div class="goods-add">
    <el-steps :active="active">
      <el-step title="步骤 1" description="基本信息"></el-step>
      <el-step title="步骤 2" description="商品图片"></el-step>
      <el-step title="步骤 3" description="商品内容"></el-step>
    </el-steps>
    <el-tabs :tab-position="tabPosition" :value="active.toString()" @tab-click="handleTabClick">
      <el-tab-pane label="基本信息">
        <el-form :model="addForm" :rules="rules" ref="addForm">
          <el-form-item label="商品名称" prop="goods_name" label-width="80px">
            <el-input placeholder="请输入商品名称" v-model="addForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price" label-width="80px">
            <el-input placeholder="请输入商品价格" v-model.number="addForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight" label-width="80px">
            <el-input placeholder="请输入商品重量" v-model.number="addForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number" label-width="80px">
            <el-input placeholder="请输入商品数量" v-model.number="addForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="goods_cat" label-width="80px">
            <el-cascader
              v-model="addForm.goods_cat"
              :options="options"
              :props="props"
              >
            </el-cascader>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="next">下一步</el-button>
      </el-tab-pane>
      <el-tab-pane label="商品图片">
        <el-upload
          :action="$axios.defaults.baseURL+'upload'"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :headers="headers"
          name='file'
          :on-success="imgUploadSuc"
          :on-error="imgUploadErr"
         >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <el-button type="primary" @click="next">下一步</el-button>
      </el-tab-pane>
      <el-tab-pane label="商品内容" class="tab-content">

        <quillEditor v-model="addForm.goods_introduce" class="quillEditor"></quillEditor>

        <el-button type="primary" @click="submitGoods" class="subBtn">添加</el-button>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  components: {
    quillEditor
  },
  data () {
    return {
      tabPosition: 'left',
      active: 0,
      addForm: {
        goods_name: '',
        goods_cat: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: '',
        pics: []
      },
      options: [],
      props: {
        label: 'cat_name',
        value: 'cat_id'
      },
      dialogImageUrl: '',
      dialogVisible: false,
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: ['blur', 'change'] },
          { min: 3, message: '长度至少为3个字符', trigger: ['blur', 'change'] }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: ['blur', 'change'] },
          { type: 'number', message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: ['blur', 'change'] },
          { type: 'number', message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: ['blur', 'change'] },
          { type: 'number', message: '请输入有效的数字', trigger: ['blur', 'change'] }
        ],
        goods_cat: [
          { required: true, message: '请选择商品分类', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  async created () {
    const { meta, data } = await this.$axios.get('categories?type=3')
    if (meta.status === 200) {
      this.options = data
      // console.log(data)
    }
  },
  methods: {
    handleTabClick (tab) {
      // console.log(tab.index)
      this.active = +tab.index
    },
    async next () {
      try {
        await this.$refs.addForm.validate()
        this.active++
      } catch (err) {
        console.log(err)
      }
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    imgUploadSuc ({ data }) {
      // console.log(data)
      this.dialogImageUrl = data.url
      this.addForm.pics.push(data.url)
    },
    imgUploadErr (err) {
      console.log(err)
    },
    async submitGoods () {
      try {
        await this.$refs.addForm.validate()
        this.addForm.goods_cat = this.addForm.goods_cat.join(',')
        const { meta } = await this.$axios.post('goods', this.addForm)
        if (meta.status === 201) {
          // console.log(data)
          this.$message.success(meta.msg)
          this.$router.push('/goods')
        } else {
          this.$message.error(meta.msg)
        }
      } catch (err) {
        console.log(err)
        this.active = 0
      }
    }
  },
  computed: {
    headers () {
      return {
        Authorization: localStorage.getItem('token')
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.goods-add{
  ::v-deep .el-tabs{
    z-index: 9;
   .tab-content{
      position: relative;
     ::v-deep .subBtn{
      // margin-top: 30px;
      // position: absolute;
      // bottom: -30px;
      // z-index: 10;
      }
    }
    .quillEditor{
      height: 300px;
      background-color: #fff;
      border-bottom: 1px solid #ccc;
    }
  }
}

</style>
